package com.example.zk2.indexfg

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.zk2.R
import com.example.zk2.adapter.GoodsAdapter
import com.example.zk2.adapter.TypeAdapter
import com.example.zk2.databinding.FragmentTjBinding
import com.example.zk2.search.SearchActivity
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader

class TjFragment : BaseFragment<FragmentTjBinding,TjViewModel>() {
    //轮播图
    val imgs = mutableListOf<String>()
    //分类
    lateinit var typeAdapter: TypeAdapter
    //商品列表  可加载更多
    lateinit var goodsAdapter: GoodsAdapter
    var page = 1
    override fun initView() {
        typeAdapter = TypeAdapter()
        binding.rvType.adapter = typeAdapter
        binding.rvType.layoutManager = GridLayoutManager(context,4)

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

        viewModel.index()
        viewModel.indexGoods(4,3,page)
        //刷新 //如果写切换，，别忘了全局的Type 需要修改 page设为1
        binding.smart.setOnRefreshLoadMoreListener(object:OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.indexGoods(4,3,page)
                binding.smart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.indexGoods(4,3,page)
                binding.smart.finishLoadMore()
            }
        })
        //跳搜索
        binding.tvSearch.setOnClickListener {
            startActivity(Intent(requireContext(),SearchActivity::class.java))
        }
    }

    override fun initData() {
        viewModel.indexSuccess.observe(this){
            if(it.code==200){
                //轮播图
                it.data.banner.forEach {
                    imgs.add(it.pic)
                }
                binding.banner.setImages(imgs)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null) {
                                if (imageView != null) {
                                    Glide.with(context).load(path).into(imageView)
                                }
                            }
                        }
                    })
                    .setDelayTime(1500)
                    .start()
                //分类
                typeAdapter.data.clear()
                typeAdapter.data.addAll(it.data.explosiveMoney)
                typeAdapter.notifyDataSetChanged()
            }

            viewModel.goodsSuccess.observe(this){
                if(it.code==200){
                    if(page==1){
                        goodsAdapter.data.clear()
                    }
                    it.data.list.forEachIndexed { index, goods ->
                        goods.type = index%2
                    }
                    goodsAdapter.data.addAll(it.data.list)
                    goodsAdapter.notifyDataSetChanged()
                    if(it.data.list.size==0){
                        ToastUtils.showLong("没有数据了")
                    }
                }
            }
        }
    }

    override fun getViewModel(): Class<TjViewModel> {
        return TjViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_tj
    }

}